<?php

namespace App\Console\Commands;

use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;

class Inpsect extends Command
{
    protected $signature = 'cc:inspect';

    protected $description = "";

    public function handle()
    {
	    $type = $this->ask("Input inspect type id: [a,b,c,d,e,f]");

	    $type = (string)$type;

	    if ($type == 'a') {
		    $this->inspectA();
	    } elseif ($type == 'b') {
		    $this->inspectB();
	    } elseif ($type == 'c') {
		    $this->inspectC();
	    } elseif ($type == 'd') {
		    $this->inspectD();
	    } elseif ($type == 'e') {
		    $this->inspectE();
	    } elseif ($type == 'f') {
		    $this->inspectF();
	    }
    }

	protected function inspectA()
	{
		$this->error('===== 余额 > 0，账户无充值操作,无充值明细,无系统转入操作,无个人转入操作 =====');

		$count = 0;

		User::query()
			->where('gold_coins', '>', 0)
			->where('is_locked', false)
			->whereDoesntHave('goldCharges', function (Builder $query) {
				$query->whereNotNull('paid_at');
			})->whereDoesntHave('goldCoins', function (Builder $query) {
				$query->where('remark', 'gold_charge')
					->orWhere('remark', 'transfered_in_coins')
					->orWhere('remark', 'system_transfer_coins');
			})->chunk(50, function (Collection $users) use (&$count) {

				if ($users->isEmpty()) return false;

				foreach ($users as $user) {
					$this->info("
ID：{$user->id}
姓名：{$user->name}
手机：{$user->phone}
余额：{$user->gold_coins}
金豆：{$user->gold_beans}
创建：{$user->created_at}");

					$count++;
				}

				$this->info("\n共计：({$count}) 人");

				return true;

			});
	}

	protected function inspectB()
	{
		$this->error('===== 余额 > 0，账户无充值操作,无充值明细,无系统转入操作 =====');

		$count = 0;

		User::query()
			->where('gold_coins', '>', 0)
			->where('is_locked', false)
			->whereDoesntHave('goldCharges', function (Builder $query) {
				$query->whereNotNull('paid_at');
			})->whereDoesntHave('goldCoins', function (Builder $query) {
				$query->where('remark', 'gold_charge')
					->orWhere('remark', 'system_transfer_coins');
			})->chunk(50, function (Collection $users) use (&$count) {

				if ($users->isEmpty()) return false;

				foreach ($users as $user) {
					$this->info("
ID：{$user->id}
姓名：{$user->name}
手机：{$user->phone}
余额：{$user->gold_coins}
金豆：{$user->gold_beans}
创建：{$user->created_at}");

					$count++;
				}

				$this->info("\n共计：({$count}) 人");

				return true;

			});
	}

	protected function inspectC()
	{
		$this->error('===== 余额 > 0，账户无充值操作,无充值明细,无个人转入操作 =====');

		$count = 0;

		User::query()
			->where('gold_coins', '>', 0)
			->where('is_locked', false)
			->whereDoesntHave('goldCharges', function (Builder $query) {
				$query->whereNotNull('paid_at');
			})->whereDoesntHave('goldCoins', function (Builder $query) {
				$query->where('remark', 'gold_charge')
					->orWhere('remark', 'transfered_in_coins');
			})->chunk(50, function (Collection $users) use (&$count) {

				if ($users->isEmpty()) return false;

				foreach ($users as $user) {
					$this->info("
ID：{$user->id}
姓名：{$user->name}
手机：{$user->phone}
余额：{$user->gold_coins}
金豆：{$user->gold_beans}
创建：{$user->created_at}");

					$count++;
				}

				$this->info("\n共计：({$count}) 人");

				return true;

			});
	}

	protected function inspectD()
	{
		$this->error('===== 余额 > 0，账户无充值操作,无充值明细 =====');

		$count = 0;

		User::query()
			->where('gold_coins', '>', 0)
			->where('is_locked', false)
			->whereDoesntHave('goldCharges', function (Builder $query) {
				$query->whereNotNull('paid_at');
			})->whereDoesntHave('goldCoins', function (Builder $query) {
				$query->where('remark', 'gold_charge');
			})->chunk(50, function (Collection $users) use (&$count) {

				if ($users->isEmpty()) return false;

				foreach ($users as $user) {
					$this->info("
ID：{$user->id}
姓名：{$user->name}
手机：{$user->phone}
余额：{$user->gold_coins}
金豆：{$user->gold_beans}
创建：{$user->created_at}");

					$count++;
				}

				$this->info("\n共计：({$count}) 人");

				return true;

			});
	}

	protected function inspectE()
	{
		$this->error('===== 余额 > 0，账户无充值操作 =====');

		$count = 0;

		User::query()
			->where('gold_coins', '>', 0)
			->where('is_locked', false)
			->whereDoesntHave('goldCharges', function (Builder $query) {
				$query->whereNotNull('paid_at');
			})->chunk(50, function (Collection $users) use (&$count) {

				if ($users->isEmpty()) return false;

				foreach ($users as $user) {
					$this->info("
ID：{$user->id}
姓名：{$user->name}
手机：{$user->phone}
余额：{$user->gold_coins}
金豆：{$user->gold_beans}
创建：{$user->created_at}");

					$count++;
				}

				$this->info("\n共计：({$count}) 人");

				return true;

			});
	}

	protected function inspectF()
	{
		$this->error('===== 余额明细 < 0 =====');

		User::query()
			->where('is_locked', false)
			->chunk(50, function (Collection $users) {

				if ($users->isEmpty()) return false;

				/**
				 * @var User $user
				 */
				foreach ($users as $user) {

					if ($user->goldCoins()->sum('money') < -0.01) {
$this->info("
ID：{$user->id}
姓名：{$user->name}
手机：{$user->phone}
余额：{$user->gold_coins}
金豆：{$user->gold_beans}
创建：{$user->created_at}"
);
					}
				}

				return true;

			});
	}
}
